package com.flyco.modules.stm.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.flyco.modules.stm.entity.StmConvert;
import com.flyco.modules.stm.entity.StmConvertItem;
import com.flyco.modules.stm.mapper.StmConvertItemMapper;
import com.flyco.modules.stm.mapper.StmConvertMapper;
import com.flyco.modules.stm.service.IStmConvertService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 门店转换单表
 * @Author: flyco
 * @Date:   2021-01-19
 * @Version: V1.0
 */
@DS("store")
@Service
public class StmConvertServiceImpl extends ServiceImpl<StmConvertMapper, StmConvert> implements IStmConvertService {

	@Autowired
	private StmConvertMapper stmConvertMapper;
	@Autowired
	private StmConvertItemMapper stmConvertItemMapper;
	
	@Override
	@Transactional
	public void saveMain(StmConvert stmConvert, List<StmConvertItem> stmConvertItemList) {
		stmConvertMapper.insert(stmConvert);
		for(StmConvertItem entity:stmConvertItemList) {
			//外键设置
			entity.setConvertId(stmConvert.getId());
			stmConvertItemMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(StmConvert stmConvert,List<StmConvertItem> stmConvertItemList) {
		stmConvertMapper.updateById(stmConvert);
		
		//1.先删除子表数据
		stmConvertItemMapper.deleteByMainId(stmConvert.getId());
		
		//2.子表数据重新插入
		for(StmConvertItem entity:stmConvertItemList) {
			//外键设置
			entity.setConvertId(stmConvert.getId());
			stmConvertItemMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		stmConvertItemMapper.deleteByMainId(id);
		stmConvertMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			stmConvertItemMapper.deleteByMainId(Long.valueOf(id.toString()));
			stmConvertMapper.deleteById(id);
		}
	}
	
}
